import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.tmatesoft.sqljet.core.SqlJetException;

public class Main {

	/**
	 * @param args
	 * @throws IOException
	 */

	public static String PATH = "/Deve/workspace/AmharicBibleDatabase/amharicbible/";
	Writer out;
	static IDatabase db;
	List<Book> books = new ArrayList<Book>();

	public static void main(String[] args) throws IOException, SqlJetException {

		db = new DatabseAdapter();

		Main main = new Main();

		File input = new File(
				"/Deve/workspace/AmharicBibleDatabase/amharicbible/Menu.htm");
		Document doc = Jsoup.parse(input, "UTF-8");

		Elements body = doc.getElementsByTag("a");
		List<Book> books = new ArrayList<Book>();
		for (Element link : body) {
			String linkHref = link.attr("href");
			String linkText = link.text();
			Book book = new Book();
			if (!(linkText == null || linkText.length() == 0)) {
				if (linkHref != null && linkHref.length() != 0)
					if (linkHref.contains("#P0_0")
							|| linkHref.contains("#TopOfPage")) {
						if (linkHref.contains("#TopOfPage")) {
							book = new Book();
							book.name = linkText;
							book.chapters = new ArrayList<Chapter>();
							books.add(book);
						}
					} else {
						int index = linkHref.indexOf("#");
						Chapter chapter = new Chapter();
						chapter.setFileName(linkHref.substring(0, index));
						// System.out.println(chapter.fileName);

						chapter.setNumber(getChapterNumber(linkText));
						getVerses(chapter);
						books.get(books.size() - 1).chapters.add(chapter);
					}

			}

		}
		for (Book book : books) {
			try {
				db.addBook(book);
				db.addVerse(book);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	static void getVerses(Chapter chapter) throws IOException {
		File input = new File(
				"/Deve/workspace/AmharicBibleDatabase/amharicbible/"
						+ chapter.getFileName());
		Document doc = Jsoup.parse(input, "UTF-8");

		Elements body = doc.getElementsByTag("font");
		Main main = new Main();
		main.write(chapter.getFileName());
		for (Element link : body) {
			String linkText = link.text();

			if (isStartsWithNumber(linkText)) {
				Verse verse = new Verse();
				verse.number = getVerseNumber(linkText);
				verse.content = linkText;
//				System.out.println(chapter.getNumber());
				verse.chapterNumber = chapter.getNumber();
				chapter.getVerses().add(verse);
				// System.out.println(verse.number);
				// System.out.println(verse.content.toString("UTF-8"));
				main.write(linkText);
			}
		}
		main.out.close();

	}

	static boolean isStartsWithNumber(String str) {
		try {
			Integer.parseInt(str.substring(0, 1));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
	}

	void write(String text) throws IOException {

		if (out == null)
			out = new OutputStreamWriter(new FileOutputStream(PATH
					+ "wholeBible.txt"), "UTF-8");
		try {
			out.write(text);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	static private int getVerseNumber(String linkText) {
		String number = "";
		for (int i = 0; i < linkText.length(); i++) {
			try {
				number = number
						+ Integer.parseInt(linkText.substring(i, i + 1));
			} catch (NumberFormatException e) {
				break;
			}
		}
		return Integer.parseInt(number);

	}

	static private int getChapterNumber(String linkText) {
		String number = "";
		boolean flag = false;
		for (int i = linkText.length() - 1; i >= 0; i--) {
			try {

				number = Integer.parseInt(linkText.substring(i, i + 1))
						+ number;
				flag = true;
			} catch (NumberFormatException e) {
				if (flag == true)
					break;
			}
		}

		return Integer.parseInt(number);
	}
}
